<?php
namespace App\Http\Controllers\Admin;


use App\Admin_authority;
use App\Admin_role_authority;
use App\Admin_user;
use App\Http\Controllers\Controller;
use App\Log;
use Illuminate\Support\Facades\Redis;

class AController extends Controller
{
    protected $redis = [];//redis数据
    protected $root = '';//root账号
    protected $account = '';//当前账号

    protected function _initialize()
    {
        $this->account = session()->get('account');

        $this->redis = unserialize(Redis::get(APP_NAME));

        $this->refresh_redis();

        @view()->share(['menus' => $this->redis[$this->account]['authority'] ? : [],'account'=>$this->account,'nickname'=>$this->redis[$this->account]['nickname']]);
    }



    //刷新Redis数据
    protected function refresh_redis($key = '')
    {
        $is_write  = 0;

        //清空指定数据
        if($key){
            //Log::log('debug',$this->redis[$key]);
            $is_write = 1;
            if( 'all' == $key){
                $this->redis = null;
            }else{
                $this->redis[$key] = null;
            }
        }

        //所有菜单
        if(!isset($this->redis['authority']) || !$this->redis['authority']){
            $is_write = 1;
            $this->redis['authority'] = $this->get_menu_tree($this->get_authority());
        }

        //root账号
        if(!isset($this->redis['root']) || !$this->redis['root'] || ($this->redis['root'] != env('ROOT'))){
            $is_write = 1;
            $this->root = $this->redis['root'] = env('ROOT');
        }else{
            $this->root = $this->redis['root'];
        }

        //当前用户信息
        if(!isset($this->redis[$this->account]) || !$this->redis[$this->account]){
            $is_write = 1;
            $this->redis[$this->account] = $this->get_userInfo();
        }

        if($is_write){
            Redis::set(APP_NAME,serialize($this->redis));
        }

    }

    //获取用户信息
    protected function get_userInfo()
    {
        $user = Admin_user::where('account','=',"$this->account")->first();

        if(!$user->role){
            return [];
        }
        $user_info = [];
        $user_info['id'] = $user->id;
        $user_info['account'] = $user->account;
        $user_info['nickname'] = $user->nickname;
        $user_info['role_id'] = $user->role->role_id;

        if($this->account == $this->root){
            //最高权限
            $authority = $this->get_authority();
        }else{
            //个人权限
            $authority = $this->get_authority($user->role->role_id);
        }

        $list = $this->get_menu_tree($authority);
        $rule = $this->get_rule($authority);

        $user_info['authority'] = $list;
        $user_info['rule'] = $rule;

        return $user_info;
    }



    //获取权限
    protected function get_authority($role_id = '')
    {
        if($role_id){
            $arr = Admin_role_authority::from('admin_role_authority as ra')
                ->join('admin_module as m','m.id','=','ra.module_id')
                ->join('admin_classify as c','c.id','=','ra.classify_id')
                ->join('admin_authority as a','a.id','=','ra.authority_id')
                ->where('ra.role_id','=',$role_id)
                ->select('a.id as a_id','a.name as a_name','a.code as a_code','a.module_id as a_module_id','a.classify_id as a_classify_id','m.name as m_name','m.icon as m_icon','c.name as c_name','c.route as c_route','c.func as c_func','c.sort AS c_sort','m.sort AS m_sort')
                ->orderBy('m.sort', 'desc')
                ->orderBy('c.sort', 'desc')
                ->orderBy('c.id', 'asc')
                ->get();
        }else{
            $arr = Admin_authority::from('admin_authority as a')
                ->join('admin_module as m','a.module_id','=','m.id')
                ->join('admin_classify as c','c.id','=','a.classify_id')
                ->select('a.id as a_id','a.name as a_name','a.code as a_code','a.module_id as a_module_id','a.classify_id as a_classify_id','m.name as m_name','m.icon as m_icon','c.name as c_name','c.route as c_route','c.func as c_func','c.sort AS c_sort','m.sort AS m_sort')
                ->orderBy('m.sort', 'desc')
                ->orderBy('c.sort', 'desc')
                ->orderBy('c.id', 'asc')
                ->get();
        }

        return $arr;
    }


    //获取用户规则
    private function get_rule($arr)
    {
        $rule = [];
        foreach($arr as $k => $v){
            $rule[$k]['route'] = $v['c_route'];
            $rule[$k]['code'] = $v['a_code'];
        }
        return $rule;
    }

    //获取菜单树状图
    private function get_menu_tree($arr)
    {
        $list = [];

        //构建菜单树状图
        foreach($arr as $k => $v){
            $list[$v['a_module_id']]['m_id'] = $v['a_module_id'];
            $list[$v['a_module_id']]['m_name'] = $v['m_name'];
            $list[$v['a_module_id']]['m_icon'] = $v['m_icon'];
            $list[$v['a_module_id']]['arr'] = [];
        }
        foreach($list as $kk => $vv){
            $s = 0;
            foreach($arr as $kkk => $vvv){
                if($vvv['a_module_id'] == $vv['m_id']){
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_id'] = $vvv['a_classify_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_name'] = $vvv['c_name'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_route'] = $vvv['c_route'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_func'] = $vvv['c_func'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['c_sort'] = $vvv['c_sort'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['is_check'] = 0;
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['name'] = $vvv['a_name'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['module_id'] = $vvv['a_module_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['classify_id'] = $vvv['a_classify_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['code'] = $vvv['a_code'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['id'] = $vvv['a_id'];
                    $list[$kk]['arr'][$vvv['a_classify_id']]['arr'][$s]['is_check'] = 0;
                }
                $s ++ ;
            }
        }
        return $list;
    }


    /**
     * 权限校验
     * @param $class /__CLASS__
     * @param $function /__FUNCTION__
     * @return bool
     */
    protected function check_rule($class,$function)
    {
        $is_yes = false;

        if($class && $function){

            $rule = $this->redis[$this->account]['rule'];

            $a = explode('\\',$class);
            $a = $a[count($a)-2].'/'.end($a);

            foreach($rule as $k => $v){
                if($a == substr($v['route'],0,strrpos($v['route'],'/')).'Controller'){
                    if($function == $v['code']){
                        $is_yes = true;
                    }
                }
            }
        }

        return $is_yes;
    }

}